package cn.gjxblog.demo.factorybean;

import org.springframework.beans.factory.FactoryBean;

import java.lang.reflect.Proxy;

/**
 * TODO...
 *
 * @Author guojiaxun
 * @Date 2019/11/8 15:04
 */
public class MyFacotyBean implements FactoryBean<Object> {

	private Class<?> interfaceClazz; //实现的接口的全类名
	private Object target; // 该接口的实现类
	private Object proxyObj;

	public MyFacotyBean(Class<?> interfaceClazz, Object target, Object proxyObj) {
		this.interfaceClazz = interfaceClazz;
		this.target = target;
		this.proxyObj = Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{interfaceClazz},(proxy,method,args) -> {
			Object result = method.invoke(target, args);
			return result;
		});
	}

	@Override
	public Object getObject() throws Exception {
		return proxyObj;// 返回这个代理对象 而不是new直接new出来的对象
	}

	@Override
	public Class<?> getObjectType() {
		return proxyObj == null ? Object.class : proxyObj.getClass();
	}


}
